package com.ajax.servlet;

import com.alibaba.fastjson.JSON;

/**
 * @author wzm
 * @create 2023-03-24 10:06
 */
public class JsonTest {
    public static void main(String[] args) {
        //1.java对象转为Json字符串
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        student.setPassword("123456");

        String s = JSON.toJSONString(student);
        System.out.println("转换后的字符串" + s);//{"id":1,"name":"张三","password":"123456"}

        //2.将字符串转为java对象
        String str = "{\"id\":1,\"name\":\"张三\",\"password\":\"123456\"}";

        Student student1 = JSON.parseObject(str, Student.class);
        System.out.println("student1 = " + student1);
    }
}
